var e = require("../../../@babel/runtime/helpers/interopRequireDefault").default, t = require("../../../@babel/runtime/helpers/objectSpread2"), a = e(require("@vant/weapp/dialog/dialog.js")), i = e(require("../../../api/api_queryRemain.js")), n = e(require("../../../api/wxRequest.js")), o = e(require("../../../utils/Tips")), r = e(require("../../../behaviors/auth")), s = e(require("../../../behaviors/openid.js")), d = require("../../../utils/util"), l = getApp(), c = l.eventBus;

Component({
    behaviors: [ r.default, s.default ],
    properties: {
        id: {
            type: String
        }
    },
    data: {
        product: {},
        id: "",
        confirmPactsChecked: !1,
        confirmPactsCheckedColor: "",
        show: !1,
        fromLive: !1,
        liveRoomId: "",
        confirmBtnLoading: !1,
        purchasers: []
    },
    methods: {
        init: function() {
            var e = this;
            o.default.loading();
            var n = this.data.id;
            i.default.getProductDetail({
                goodId: n
            }).then(function(i) {
                if (console.log(i), o.default.loaded(), "0" === i.code) {
                    if (i.data) {
                        var n = i.data.goods ? i.data.goods[0] : void 0;
                        if (e.setData({
                            purchasers: i.data.purchaseUsers || []
                        }), (0, d.isUnd)(n)) return void a.default.alert({
                            context: e,
                            message: "查询不到此商品"
                        });
                        var r = n.begintime, s = n.shareTip, l = Date.now() > r, c = "";
                        l || (e.setSaleTime(r), c = (0, d.dateFormat)(new Date(r), "MM-DD HH:mm"), e.intervalTimer = setInterval(function() {
                            Date.now() >= r && (clearInterval(e.intervalTimer), e.intervalTimer = null, e.setData({
                                "product.onSale": !0
                            }));
                        }, 1e3)), wx.setNavigationBarTitle({
                            title: n.name
                        }), e.setData({
                            product: t(t({}, n), {}, {
                                onSale: l,
                                formatBeginTime: c
                            }),
                            show: !0
                        }), s && e.triggerEvent("sharemsgchange", {
                            title: s
                        }), -1 === [ 1, 2, 8, 9 ].indexOf(n.classify) && wx.hideShareMenu();
                    }
                } else a.default.alert({
                    context: e,
                    message: i.message
                });
            }).catch(function() {
                o.default.loaded();
            });
        },
        handleConfirm: function() {
            var e = this, t = this.data.product, r = t.classify, s = t.tip, d = t.copyTip, u = t.goodId, f = t.name, m = t.appid, h = t.path, p = t.jumpUrl;
            if ("1007000" !== u) if (8 === r) wx.navigateToMiniProgram({
                appId: m,
                path: h,
                fail: function(e) {
                    console.log(e), o.default.toast("跳转失败");
                }
            }); else if (9 === r) p ? (l.eventBus.emit("toWebviewPage", {
                url: p
            }), wx.navigateTo({
                url: "/pages/webview/index"
            })) : o.default.toast("无法跳转"); else if (2 === r) a.default.alert({
                context: this,
                confirmButtonText: "立即复制",
                richTextMessage: !0,
                richText: [ {
                    name: "div",
                    children: [ {
                        type: "text",
                        text: "请复制如下信息："
                    } ]
                }, {
                    name: "div",
                    children: [ {
                        type: "text",
                        text: s
                    } ]
                } ]
            }).then(function() {
                var e = d || s;
                n.default.setClipboardData(e).catch(function() {
                    o.default.toast("复制失败，请重试");
                });
            }); else if (1 === r) c.emit("toOrderConfirmPageInit", this.data.product, this.query), 
            wx.navigateTo({
                url: "/pages/orderConfirm/orderConfirm"
            }); else {
                if (4 === r) if (!this.testConfirmPact()) return void a.default.alert({
                    context: this,
                    message: "请先阅读并同意协议内容"
                });
                this.setData({
                    confirmBtnLoading: !0
                }, function() {
                    i.default.confirmOwnBusiness({
                        goodId: u
                    }).then(function(t) {
                        e.setData({
                            confirmBtnLoading: !1
                        }), "3001" === t.code ? a.default.confirm({
                            context: e,
                            confirmButtonText: "立即绑定",
                            confirmButtonOpenType: "getPhoneNumber",
                            message: "您当前登录的号码不是王卡号码，无法办理该项业务，请点击“使用其他号码”，使用腾讯王卡绑定后再次重试。"
                        }) : "10002" === t.code ? a.default.confirm({
                            context: e,
                            confirmButtonText: "立即登录",
                            confirmButtonOpenType: "getPhoneNumber",
                            message: "您还未登录“王卡助手”小程序，请登录后提交"
                        }) : "0" !== t.code ? a.default.alert({
                            context: e,
                            message: t.message
                        }) : a.default.alert({
                            context: e,
                            message: "“".concat(f, "”受理成功，正在为您办理中")
                        });
                    }).catch(function() {
                        e.setData({
                            confirmBtnLoading: !1
                        });
                    });
                });
            } else wx.navigateToMiniProgram({
                appId: "wxd5d697a50844ca1b",
                path: "packages/goods/detail/index?alias=3npcjsjnzc2kp",
                fail: function(e) {
                    console.log(e), o.default.toast("跳转失败");
                }
            });
        },
        testConfirmPact: function() {
            return this.data.confirmPactsChecked;
        },
        setSaleTime: function(e) {
            var t = this;
            this.saleInterval = setInterval(function() {
                Date.now() > e && (t.setData({
                    "product.onSale": !0
                }), clearInterval(t.saleInterval));
            });
        },
        handleLiveFloat: function() {
            var e = this, t = this.data.liveRoomId;
            this.getOpenid(t).then(function(t) {
                l.kta.clickStat("7140e23ffe244e9f8943767691e0b29b", {
                    roomId: e.data.liveRoomId,
                    openid: t
                }), wx.navigateTo({
                    url: "plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=".concat(e.data.liveRoomId)
                });
            }).catch(function() {
                l.kta.clickStat("7140e23ffe244e9f8943767691e0b29b", {
                    roomId: e.data.liveRoomId
                }), wx.navigateTo({
                    url: "plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=".concat(e.data.liveRoomId)
                });
            });
        },
        checkboxChange: function() {
            var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {
                detail: {}
            }, t = e.detail.value;
            this.setData({
                confirmPactsChecked: -1 !== t.indexOf("yes")
            });
        },
        handleGoToSuperPact: function() {
            wx.navigateTo({
                url: "/pages/superVipPact/index"
            });
        }
    },
    attached: function() {
        var e = this;
        try {
            this.sub1 = c.on("toProductPageInit", function(t) {
                var a = t.id, i = t.room_id;
                e.setData({
                    id: a,
                    fromLive: !!i,
                    liveRoomId: i
                }, function() {
                    e.init();
                });
            }), this.sub2 = c.on("productPageRefresh", function() {
                e.init();
            });
        } catch (e) {
            e = VM2_INTERNAL_STATE_DO_NOT_USE_OR_PROGRAM_WILL_FAIL.handleException(e);
            console.log(e);
        }
    },
    detached: function() {
        this.intervalTimer && (clearInterval(this.intervalTimer), this.intervalTimer = null), 
        this.sub1 && this.sub1.off(), this.sub2 && this.sub2.off();
    }
});